<template>
  <div>
    <el-button type="primary" @click="onAdd">添加</el-button>
    <el-table
        :data="userList"
        style="width: 100%">
      <el-table-column
          prop="birthday"
          label="日期"
          width="180">
      </el-table-column>
      <el-table-column
          prop="realName"
          label="姓名"
          width="180">
      </el-table-column>
      <el-table-column
          prop="account"
          label="账号">
      </el-table-column>
      <el-table-column
          prop="roleId"
          label="角色">
      </el-table-column>
      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="onDel(scope.row.id)">删除</el-button>
          <el-button size="mini" type="primary" @click="onUpdate(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        layout="prev, pager, next"
        @current-change="onChangePage"
        :page-size="queryForm.pageSize"
        :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form size="mini" :model="form" label-width="80px">
        <el-form-item label="账号">
          <el-input v-model="form.account" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.realName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker v-model="form.birthday" value-format="yyyy-MM-dd" type="date" placeholder="生日"></el-date-picker>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleId" placeholder="角色">
            <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {list, get, del, update, add} from "@/api/user";
import {getAll} from "@/api/role";

export default {
  name: "index",
  data() {
    return {
      userList: [],
      roleList: [],
      total: 0,
      queryForm: {
        pageNum: 1,
        pageSize: 2
      },
      form: {},
      dialogFormVisible: false,
      title: '',
    }
  },
  created() {
    this.getList();
    getAll().then(res => {
      this.roleList = res.data;
    })
  },
  methods: {
    getList() {
      list(this.queryForm).then(res => {
        this.userList = res.data.list;
        this.total = res.data.total;
      })
    },
    onChangePage(pageNum) {
      this.queryForm.pageNum = pageNum;
      this.getList();
    },
    onDel(id) {
      this.$confirm('确定删除吗？', '提示', {type: 'warning'}).then(() => {
        del(id).then(() => {
          this.$message.success('删除成功');
          this.getList();
        })
      })
    },
    onAdd() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    onUpdate(id) {
      this.dialogFormVisible = true;
      get(id).then(res => {
        this.form = res.data;
      })
    },
    onSubmit() {
      if (this.form.id) {
        update(this.form).then(() => {
          this.$message.success('修改成功');
          this.getList();
        })
      } else {
        add(this.form).then(() => {
          this.$message.success('添加成功');
          this.getList();
        })
      }
    }
  }
}
</script>

<style scoped>

</style>